package com.symone.webcms.service.impl;

import com.symone.webcms.model.Message;
import com.symone.webcms.mapper.MessageMapper;
import com.symone.webcms.model.vo.MessageVO;
import com.symone.webcms.service.MessageService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xiaoleilu.hutool.util.NumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 *　　
 *   @description : Message 服务实现类
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-13
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    public Page<Message> findPage(Page<Message> page, Wrapper<Message> message) {
        List<Message> result = baseMapper.selectPage(page,message);
        page.setRecords(result);
        return page;
    }

    @Override
    public List<Message> findAll() {
        Wrapper<Message> ew = new EntityWrapper<Message>();
        ew.where("deleted=0");
        return baseMapper.selectList(ew);
    }

    @Override
    public int delete(long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public boolean deleteFlagById(long id) {
        return baseMapper.deleteFlagById(id);
    }

    @Override
    public int clear() {
        return baseMapper.delete(new EntityWrapper<Message>().eq("deleted" ,1));
    }

    @Override
    public List<MessageVO> getMessageRecieveBox(Long toId,Integer isRead) {
        return baseMapper.getMessageBox(null,toId,isRead);
    }

    @Override
    public List<MessageVO> getMessageSendBox(Long fromId,Integer isRead) {
        return baseMapper.getMessageBox(fromId,null,isRead);
    }

    @Override
    public Page<MessageVO> getMessageRecieveBoxPage(Page<MessageVO> page, Long toId, Integer isRead) {
        List<MessageVO> result = baseMapper.getMessageBox(page,null,toId,isRead);
        page.setRecords(result);
        return page;
    }

    @Override
    @Transactional
    public MessageVO getMessageVOById(Long id,Long userId) {
        MessageVO msgVO = baseMapper.getMessageVOById(id);
        if(msgVO.getToId().equals(userId) && msgVO.getIsRead() == 0) {
            Message message = new Message();
            message.setId(id);
            message.setFromId(msgVO.getFromId());
            message.setToId(msgVO.getToId());
            message.setTitle(msgVO.getTitle());
            message.setContent(msgVO.getContent());
            message.setIsRead(1);
            message.setUpdateTime(new Date());
            baseMapper.updateById(message);
        }
        return msgVO;
    }

    @Override
    public Page<MessageVO> getMessageSendBoxPage(Page<MessageVO> page, Long fromId, Integer isRead) {
        List<MessageVO> result = baseMapper.getMessageBox(page,fromId,null,isRead);
        page.setRecords(result);
        return page;
    }

    @Override
    public boolean batchDelete(String ids) {
        for (String id : ids.split(",")) {
            if (NumberUtil.isNumber(id)) {
                baseMapper.deleteById(Long.valueOf(id));
            } else {
                return false;
            }
        }
        return true;
    }

}
